import {
    graphql,
    GraphQLSchema,
    GraphQLObjectType,
    GraphQLString,
    GraphQLList,
    GraphQLNonNull,
    GraphQLBoolean,
    GraphQLInt,
    GraphQLInputObjectType,
    GraphQLInputType

} from 'graphql';

import Role from '../../db/models/role'
import Role_jurisdiction_temp from '../../db/models/role_jurisdiction_temp'
import User_role_temp from '../../db/models/user_role_temp'
import {
    getUserRoleQuery
} from '../../db/query'

export let roleType = new GraphQLObjectType({
    name: 'role',
    fields: {
        id: {
            type: GraphQLInt
        },
        role_name: {
            type: GraphQLString
        }
    }
})

export const roleList = {
    type: new GraphQLList(roleType),
    args: {},
    resolve(root, params, options) {
        return Role.findAll()
    }
}

export const roleOne = {
    type: roleType,
    args: {
        id: {
            type: GraphQLInt
        }
    },
    resolve(root, params, options) {
        return Role.findOne({
            where: {
                id: params.id
            }
        })
    }
}
export const roleFind = {
    type: new GraphQLList(roleType),
    args: {
        id: {
            type: GraphQLInt
        }
    },
    resolve(root, params, options) {
        return getUserRoleQuery(params)
    }
}
export const roleUpdate = {
    type: GraphQLInt,
    args: {
        id: {
            type: GraphQLInt
        },
        role_name: {
            type: GraphQLString
        }
    },
    resolve(root, params, options) {
        return Role.update({
            role_name: params.role_name
        }, {
            where: {
                id: params.id
            }
        })
    }
}

export const roleAdd = {
    type: roleType,
    args: {
        role_name: {
            type: GraphQLString
        }
    },
    resolve(root, params, options) {
        return Role.create({
            role_name: params.role_name
        })
    }
}

export const roleDel = {
    type: GraphQLInt,
    args: {
        id: {
            type: GraphQLInt
        }
    },
    resolve(root, params, options) {
        User_role_temp.destroy({
            where: {
                role_id: params.id
            }
        })
        Role_jurisdiction_temp.destroy({
            where: {
                role_id: params.id
            }
        })
        return Role.destroy({
            where: {
                id: params.id
            }
        })
    }
}




export const role_jurisdiction_ChangeOne = {
    type: GraphQLInt,
    args: {
        id: {
            type: GraphQLInt
        }
    },
    resolve(root, params, options) {
        return Role_jurisdiction_temp.destroy({
            where: {
                role_id: params.id
            }
        })
    }
}

export let role_jurisdiction_Type = new GraphQLInputObjectType({
    name: 'Role_jurisdiction_temp',
    fields: {

        jurisdiction_id: {
            type: GraphQLInt,
        },
        role_id: {
            type: GraphQLInt,
        }
    }
})

export let role_jurisdiction_Output_Type = new GraphQLObjectType({
    name: 'Role_jurisdiction_output',
    fields: {

        jurisdiction_id: {
            type: GraphQLInt,
        },
        role_id: {
            type: GraphQLInt,
        }
    }
})

export const role_jurisdiction_ChangeTow = {
    type: new GraphQLList(role_jurisdiction_Output_Type),
    args: {
        record: {
            type: new GraphQLList(role_jurisdiction_Type),
        }
    },
    resolve(root, params, options) {
        return Role_jurisdiction_temp.bulkCreate(
            params.record, {
                individualHooks: true
            }
        )

    }
}
export const role_user_ChangeOne = {
    type: GraphQLInt,
    args: {
        id: {
            type: GraphQLInt
        }
    },
    resolve(root, params, options) {
        return User_role_temp.destroy({
            where: {
                role_id: params.id
            }
        })
    }
}

export let role_user_Type = new GraphQLInputObjectType({
    name: 'Role_user_temp',
    fields: {

        user_id: {
            type: GraphQLInt,
        },
        role_id: {
            type: GraphQLInt,
        }
    }
})

export let role_user_Output_Type = new GraphQLObjectType({
    name: 'Role_user_output',
    fields: {

        user_id: {
            type: GraphQLInt,
        },
        role_id: {
            type: GraphQLInt,
        }
    }
})

// export const role_user_ChangeTow = {
//     type: GraphQLInt,
//     args: {
//         role_id: {
//             type: GraphQLInt
//         },
//         user_id: {
//             type: GraphQLInt
//         }
//     },
//     resolve(root, params, options) {
//         return User_role_temp.create({
//             role_id: params.role_id,
//             user_id: params.user_id
//         })
//     }
// }
export const role_user_ChangeTow = {
    type: new GraphQLList(role_user_Output_Type),
    args: {
        record: {
            type: new GraphQLList(role_user_Type),
        }
    },
    resolve(root, params, options) {
        return User_role_temp.bulkCreate(
            params.record, {
                individualHooks: true
            }
        )

    }
}
